<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" class="demo-form-inline" @submit.native.prevent>
      <el-form-item label="用户名">
        <el-input v-model="listQuery.q_userName" style="width: 200px;" size="small" class="filter-item" />
      </el-form-item>
      <el-form-item label="咨询状态">
        <el-select v-model="listQuery.q_state" size="small" placeholder="请选择">
          <el-option
            v-for="item in commState"
            :label="item.name"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商家">
        <el-select v-model="listQuery.q_sellerId" size="small" placeholder="请选择">
          <el-option
            v-for="item in sellerList"
            :label="item.name"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="this.getList">查询</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="rest">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="button-table-margin">
      <el-button size="small" type="primary" @click="stateYes"  v-permission="['admin/member/productask/pass']">通过</el-button>
      <el-button size="small" type="primary" @click="stateNo"  v-permission="['admin/member/productask/pass']">驳回</el-button>
    </div>
    <el-table
      ref="singleTable"
      :data="dataList"
      v-loading="listLoading"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
      style="width: 100%">
      <el-table-column
        type="selection"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        label="咨询商品"
        width="400"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <div>
          <img width="80" height="80" :src="$imagePath+scope.row.productLeadLittle" style="vertical-align: middle;">
          {{ scope.row.productName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        property="userName"
        width="150"
        label="用户名">
      </el-table-column>
      <el-table-column
        property="sellerName"
        width="150"
        label="商家">
      </el-table-column>
      <el-table-column
        property="askContent"
        width="300"
        label="咨询内容">
      </el-table-column>
      <el-table-column
        property="replyName"
        width="150"
        label="回复人员">
      </el-table-column>
      <el-table-column
        property="replyContent"
        width="300"
        label="回复内容">
      </el-table-column>
      <el-table-column
        property="createTime"
        width="200"
        label="咨询时间">
      </el-table-column>
      <el-table-column
        property="replyTime"
        width="200"
        label="回复时间">
      </el-table-column>
      <el-table-column
        property="state"
        width="150"
        label="处理状态">
        <template slot-scope="scope">
          {{ codeBox.PRODUCT_ASK_STATE[scope.row.state] }}
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { fetchProductAsk, fetchProductAskList, passProductAsk, rejectProductAsk} from '@/api/memberAsk'
import Pagination from '@/components/Pagination'
import { Message } from 'element-ui'
import { getCodeManager,codeToArray } from '@/api/all'
export default {
  name: 'productasklist',
  components: { Pagination },
  data() {
    return {
      dataList: [],
      commState: [],
      sellerList: [],
      total: 0,
      currentRow: [],
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        q_userName: '',
        q_state: '',
        q_sellerId: ''
      }
    }
  },
  created() {
    getCodeManager('PRODUCT_ASK_STATE', () => {
      this.commState = codeToArray('PRODUCT_ASK_STATE');
    })
    this.getList()
    fetchProductAsk().then(response => {
      this.sellerList=[
        ...response.data.data.sellers.map((item)=>{return  {
          name:item.sellerName,
          value:item.id
        }})
      ]
    })
  },
  methods:{
    handleSelectionChange(val) {
      this.currentRow = val
    },
    // 获取列表数据
    getList() {
      this.listLoading = true
      fetchProductAskList(this.listQuery).then(response => {
        this.dataList = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    check() {
      this.getList()
    },
    rest() {
      this.listQuery = {
        page: 1,
        limit: 20,
        q_userName: '',
        q_state: '',
        q_sellerId: ''
      }
      this.getList()
    },
    stateYes() {
      let ids = "";
      if(this.currentRow.length == 0) {
        Message.error('请选择一条记录')
        return
      }
      for (let row in this.currentRow) {
        if(this.currentRow[row].state != 2) {
          Message.error('只有处于商家回复状态下，才能进行审核操作')
          return
        }
        if((Number(row)+1) != this.currentRow.length) {
            ids += this.currentRow[row].id + ",";
        } else {
            ids += this.currentRow[row].id;
        }
      }
      console.log("ids:" + ids)
      this.$confirm('确定要审核通过吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        passProductAsk({ids: ids}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    },
    stateNo() {
      let ids = "";
      if(this.currentRow.length == 0) {
        Message.error('请选择一条记录')
        return
      }
      for (let row in this.currentRow) {
        if(this.currentRow[row].state != 2) {
          Message.error('只有处于商家回复状态下，才能进行审核操作')
          return
        }
        if((Number(row)+1) != this.currentRow.length) {
            ids += this.currentRow[row].id + ",";
        } else {
            ids += this.currentRow[row].id;
        }
      }
      this.$confirm('确定要将商家的申请驳回吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        rejectProductAsk({ids: ids}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    }
  }
}
</script>

<style>
  .filter-container .el-col-6 {
    margin-bottom: 15px;
  }
  .filter-item {
    margin-bottom: 0
  }
  /deep/ .filter-container label {
    font-size: 14px;
    font-weight: 500;
    margin-right: 10px;
  }
</style>
